package org.softmed.rest.editor.comps;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
import org.restlet.Client;
import org.restlet.data.MediaType;
import org.restlet.data.Protocol;
import org.restlet.data.Response;
import org.restlet.resource.StringRepresentation;
import org.softmed.rest.editor.EditorUtil;
import org.softmed.rest.editor.commons.SwingUtil;
import org.softmed.rest.editor.commons.URIProvider;
import org.softmed.rest.editor.config.HttpMethod;
import org.softmed.rest.editor.xml.XStreamConfigurator;

import com.thoughtworks.xstream.XStream;

public abstract class HttpMethodEditor extends SimpleEditor implements ActionListener {

	protected HttpMethod httpMethod;

	protected JComboBox name = new JComboBox(new String[] { "GET", "POST",
			"PUT", "DELETE" });
	protected JCheckBox active = new JCheckBox("Active");
	protected JLabel id = new JLabel("id");

	protected boolean settingValues = false;

	public HttpMethodEditor(URIProvider uriProvider) {
		super(uriProvider);
		uri = "http-methods";

		XStreamConfigurator.configureStreamerHttpMethod(streamer);
		XStreamConfigurator.configureStreamerHandler(streamer);
		
		
		name.setSelectedIndex(0);

		active.addActionListener(this);
		name.addActionListener(this);
		// setup();
	}

	// public HttpMethodEditor(URIProvider uriProvider) {
	// super(uriProvider);
	// uri = "http-methods";
	//
	// XStreamConfigurator.configureStreamerHttpMethod(streamer);
	// name.setSelectedIndex(0);
	// this.uriProvider = uriProvider;
	// SwingUtil.setSize(this, EditorUtil.smallestWidth,
	// EditorUtil.smallestHeight);
	// MigLayout layout = new MigLayout("left, insets " +
	// EditorUtil.smallestInsets);
	// setLayout(layout);
	//
	// // active.get
	//
	// add(id, "w 25!");
	// add(active, "w 20!");
	// add(name, "w 70!, h 20!");
	//
	// active.addActionListener(this);
	// name.addActionListener(this);
	//
	// }

	public void createNew() throws Throwable {
		httpMethod = new HttpMethod();
		httpMethod.setName((String) name.getSelectedItem());

		String xml = streamer.toXML(httpMethod);
		Response response = client.post(getClassURI(),
				new StringRepresentation(xml, MediaType.APPLICATION_XML));
		if (response.getStatus().getCode() != 201)
			throw new RuntimeException("Error creating Http Method with xml;"
					+ xml);
		xml = response.getEntity().getText();
		httpMethod = (HttpMethod) streamer.fromXML(xml);
		setEntity(httpMethod);

	}

	public void setEntity(Object hm) throws Throwable {
		settingValues = true;
		this.httpMethod = (HttpMethod) hm;
		active.setSelected(httpMethod.isActive());
		name.setSelectedItem(httpMethod.getName());
		id.setText(httpMethod.getId().toString());
		settingValues = false;
		repaint();
	}

	public void loadEntity(String uri) throws Throwable {
		Response response = client.get(uri);
		if (response.getStatus().getCode() != 200)
			throw new RuntimeException("Error fetching entity " + uri);
		String xml = response.getEntity().getText();
		httpMethod = (HttpMethod) streamer.fromXML(xml);
		setEntity(httpMethod);
	}

	public void refresh() throws Throwable {
		loadEntity(httpMethod.getUri());
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		super.actionPerformed(e);
		
		
		if (!editable)
			return;

		if (settingValues)
			return;
		try {
			Object source = e.getSource();
			if (source == active) {
				active.setForeground(Color.RED);
				Response response = client.put(getInstanceURI() + "/"
						+ "active", new StringRepresentation(active
						.isSelected()
						+ ""));
				if (response.getStatus().getCode() != 200)
					throw new RuntimeException(
							"Error changing app active state to "
									+ active.isSelected());
				active.setForeground(Color.BLACK);
			} else if (source == name) {
				name.setForeground(Color.RED);
				Response response = client.put(getInstanceURI() + "/" + "name",
						new StringRepresentation(name.getSelectedItem() + ""));
				if (response.getStatus().getCode() != 200)
					throw new RuntimeException(
							"Error changing Http Method active state to "
									+ active.isSelected());
				name.setForeground(Color.BLACK);
			}
		} catch (Throwable t) {
			t.printStackTrace();
			JOptionPane.showMessageDialog(this, t.getMessage(), "Error",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	private String getInstanceURI() {
		return getClassURI() + "/" + httpMethod.getId();
	}

	public void addMouseListener(MouseListener list) {
		super.addMouseListener(list);
		active.addMouseListener(list);
		name.addMouseListener(list);
	}

	public void removeMouseListener(MouseListener list) {
		super.removeMouseListener(list);
		active.removeMouseListener(list);
		name.removeMouseListener(list);
	}

	public void setEditable(boolean editable) {
		this.editable = editable;
		active.setEnabled(editable);
		name.setEnabled(editable);
	}

	@Override
	public Object getEntity() {
		return httpMethod;
	}

}
